package com.ssh.cp.dao;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.ssh.cp.entity.FileEntity;
import com.ssh.cp.idao.IFileDao;

@Repository
@Transactional
public class FileDao implements IFileDao{
	@Resource(name="sessionFactory")  
	private SessionFactory sessionFactory;
	
	@Override
	public String saveFile(FileEntity inserFile) {
		// TODO Auto-generated method stub
		Session session =sessionFactory.getCurrentSession();
		try {
		  
        session.save(inserFile);
        
		}catch(RuntimeException re) {			
			throw re;
		}
		return null;
	}

	@Override
	public List<?> listAllFile(int start,int length) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.openSession(); 
		Query<?> query=null;
		List<?> list=null;
		
 		try {
		query=session.createQuery("from file");
		  query.setFirstResult(start);
          query.setMaxResults(length);
          list=query.list();
		}catch(RuntimeException re) {			
			throw re;
		}finally {
			session.close();
		}
		return list;
	}

	@Override
	public int getFileNum() {
		// TODO Auto-generated method stub
		int i=0;
		Session session =sessionFactory.openSession();
		try {
			
			Query<?> query=session.createQuery("select count(*) from file");
			i=Integer.valueOf(query.list().get(0).toString());
			
		}catch(RuntimeException re) {			
			throw re;
		}finally {
			session.close();
		}
		return i;
	}



	@Override
	public List<?> listFileByUserId(int userid, int start, int length) {
		// TODO Auto-generated method stub
		
		Session session = sessionFactory.openSession(); 
		Query<?> query=null;
		List<?> list=null;
 		try {
		query=session.createQuery("from file where userid=?");
		  query.setParameter(0, userid);
		  query.setFirstResult(start);
          query.setMaxResults(length);
          list=query.list();
		}catch(RuntimeException re) {			
			throw re;
		}finally {
			session.close();
		}
		return list;
	}

	@Override
	public int getFileNum(int userid) {
		// TODO Auto-generated method stub
		int i=0;
		Session session =sessionFactory.openSession();
		try {
			
			Query<?> query=session.createQuery("select count(*) from file where userid=?");
			query.setParameter(0, userid);
			i=Integer.valueOf(query.list().get(0).toString());
			
		}catch(RuntimeException re) {			
			throw re;
		}finally {
			session.close();
		}
		return i;
	}

	@Override
	public List<?> listFileByFileName(String filename, int start, int length) {
		// TODO Auto-generated method stub

		Session session = sessionFactory.openSession(); 
		Query<?> query=null;
		List<?> list=null;
 		try {
		query=session.createQuery("from file where filename like :filename");
		  query.setParameter("filename", "%"+filename+"%");
		  query.setFirstResult(start);
          query.setMaxResults(length);
          list=query.list();
		}catch(RuntimeException re) {			
			throw re;
		}finally {
			session.close();
		}
		return list;
	}

	@Override
	public List<?> listFileByFileName(int userid, String filename, int start, int length) {
		// TODO Auto-generated method stub
		
		Session session = sessionFactory.openSession(); 
		Query<?> query=null;
		List<?> list=null;
 		try {
		query=session.createQuery("from file where userid=? and filename like :filename ");
		  query.setParameter(0, userid);
		  query.setParameter("filename", "%"+filename+"%");
		  query.setFirstResult(start);
          query.setMaxResults(length);
          list=query.list();
		}catch(RuntimeException re) {			
			throw re;
		}finally {
			session.close();
		}
		return list;
		
		
	}



	@Override
	public List<?> listFileByUserAndClass(int userid, int classid, int start, int length) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.openSession(); 
		Query<?> query=null;
		List<?> list=null;
 		try {
		query=session.createQuery("from file where userid=? and classid=? ");
		  query.setParameter(0, userid);
		  query.setParameter(1, classid);
		  query.setFirstResult(start);
          query.setMaxResults(length);
          list=query.list();
		}catch(RuntimeException re) {			
			throw re;
		}finally {
			session.close();
		}
		return list;
	}

	@Override
	public List<?> listFileByLibrary(int userid, int libraryid, int start, int length) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.openSession(); 
		Query<?> query=null;
		List<?> list=null;
 		try {
		query=session.createQuery("from file where userid=? and libraryid=? ");
		  query.setParameter(0, userid);
		  query.setParameter(1, libraryid);
		  query.setFirstResult(start);
          query.setMaxResults(length);
          list=query.list();
		}catch(RuntimeException re) {			
			throw re;
		}finally {
			session.close();
		}
		return list;
		
	}

	
	public List<?> combinedQuery(int userid, int classid, int libraryid, int download, int share, int start,
			int length) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.openSession(); 
		Query<?> query=null;
		List<?> list=null;
 		try {
		query=session.createQuery("from file where userid=? and classid=? and libraryid=? and download=? and share=?");
		  query.setParameter(0, userid);
		  query.setParameter(1, classid);
		  query.setParameter(2, libraryid);
		  query.setParameter(3, download);
		  query.setParameter(4, share);
		  query.setFirstResult(start);
          query.setMaxResults(length);
          list=query.list();
		}catch(RuntimeException re) {			
			throw re;
		}finally {
			session.close();
		}
		return list;
	}

	@Override 
	public String updateFile(FileEntity updateFile) {
		// TODO Auto-generated method stub
		//Session session =sessionFactory.openSession();
		Session session =sessionFactory.getCurrentSession();
		/*
		try {
		  
        session.merge(updateFile);
        
		}catch(RuntimeException re) {			
			throw re;
		}
		*/
		Query<?> query=null;
		try {
			query=session.createQuery("update file set filename=?,savepath=?,share=?,download=? where fileid=? ");
			query.setParameter(0, updateFile.getFilename());
			query.setParameter(1,updateFile.getSavepath());
			query.setParameter(2,updateFile.getShare());
			query.setParameter(3,updateFile.getDownload());
			query.setParameter(4,updateFile.getFileid());
			
			//session.joinTransaction();
			query.executeUpdate();
			
		}catch(RuntimeException re) {			
			throw re;
		}
		
		
		return null;
	}

	@Override
	public List<?> listShareFile(int start,int length) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.openSession(); 
		Query<?> query=null;
		List<?> list=null;
		
 		try {
		query=session.createQuery("from file where share=1 order by uploadtime desc");
		  query.setFirstResult(start);
          query.setMaxResults(length);
          list=query.list();
		}catch(RuntimeException re) {			
			throw re;
		}finally {
			session.close();
		}
		return list;
	}

	@Override
	public int getShareFileNum() {
		// TODO Auto-generated method stub
		Session session = sessionFactory.openSession(); 
		Query<?> query=null;
		List<?> list=null;
		
 		try {
		query=session.createQuery("from file where share=1");

          list=query.list();
		}catch(RuntimeException re) {			
			throw re;
		}finally {
			session.close();
		}
		return list.size();
		
	}

	@Override
	public List<?> listShareFile(String search, int start, int length) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.openSession(); 
		Query<?> query=null;
		List<?> list=null;
 		try {
		query=session.createQuery("from file where filename like :filename and share=1");
		  query.setParameter("filename", "%"+search+"%");
		  query.setFirstResult(start);
          query.setMaxResults(length);
          list=query.list();
		}catch(RuntimeException re) {			
			throw re;
		}finally {
			session.close();
		}
		return list;

	}

	@Override
	public String checkShareAndDownload(int fileid) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.openSession(); 
		
		Query<?> query=null;
		List<?> list=null;
 		try {
		query=session.createQuery("from file where download=1 and share=1 and fileid=? ");
		  query.setParameter(0,fileid);

          list=query.list();
		}catch(RuntimeException re) {			
			throw re;
		}finally {
			session.close();
		}
 		
 		
		if(list.isEmpty())
			return null;
		else {
			FileEntity f=(FileEntity)list.get(0);
			return f.getFilename();
		}
			
	}

	@Override
	public String getFilePathById(int fileid) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.openSession(); 
		List<?> list=null;
		Query<?> query=null;
		try {
			query=session.createQuery("from file where fileid=? ");
			  query.setParameter(0,fileid);

	          list=query.list();
			}catch(RuntimeException re) {			
				throw re;
			}finally {
				session.close();
			}
		FileEntity f=(FileEntity)list.get(0);
		return f.getSavepath();
	}

	@Override
	public void delFile(int fileid) {
		// TODO Auto-generated method stub
		Session session =sessionFactory.getCurrentSession();
		Query<?> query=null;
		try {
			query=session.createQuery("delete from file where fileid=?");
			query.setParameter(0, fileid);
			query.executeUpdate();
        
		}catch(RuntimeException re) {			
			throw re;
		}
		
		
	}

	@Override
	public void delFile(int userid, int fileid) {
		// TODO Auto-generated method stub
		Session session =sessionFactory.getCurrentSession();
		Query<?> query=null;
		try {
			query=session.createQuery("delete from file where fileid=? and userid=?");
			query.setParameter(0, fileid);
			query.setParameter(1, userid);
			query.executeUpdate();
        
		}catch(RuntimeException re) {			
			throw re;
		}
	}

	@Override
	public List<?> listAllUserFile(int userid) {
		// TODO Auto-generated method stub
		
		Session session = sessionFactory.openSession(); 
		List<?> list=null;
		Query<?> query=null;
		try {
			query=session.createQuery("from file where userid=? ");
			  query.setParameter(0,userid);

	          list=query.list();
			}catch(RuntimeException re) {			
				throw re;
			}finally {
				session.close();
			}
		
		
		return list;
	}





	
	
}
